<template>
  <!-- 弹框组件 -->
  <div v-show="popupShow" @mousemove="move($event)" onselectstart = "return false">
    <div class="popup_container" ref="popup">
      <div class="popup_title" @mousedown="down($event)" @mouseup="up">信息</div>
      <div class="popup_content">{{text}}</div>
      <div class="popup_determine clearfix" @click="popupShow = false"><span>确定</span></div>
      <span class="popup_x" @click="popupShow = false"></span>
    </div>
    <!-- 覆盖层 -->
    <div class="popup_cover"></div>
  </div>
</template>

<script>
export default {
  name: 'Popup',
  data() {
    return {
      popupShow: false, // 弹窗组件是否显示
      // 拖住弹窗在屏幕可移动的最大最小距离
      minX: 0,
      minY: 0,
      maxX: 0,
      maxY: 0,
      // 是否拖动弹窗
      isMove: false,
      // 显示的文本
      text: ""
    }
  },
  methods: {
    // 按住鼠标时获取初始信息
    down(e) {
      this.minX = e.x - (e.target.parentNode.offsetLeft - e.target.parentNode.clientWidth / 2)
      this.minY = e.y - (e.target.parentNode.offsetTop - e.target.parentNode.clientHeight / 2)
      this.maxX = document.body.clientWidth - this.$refs.popup.offsetWidth + this.minX
      this.maxY = document.body.clientHeight - this.$refs.popup.offsetHeight + this.minY
      this.isMove = true
    },
    // 按住后拖到鼠标移动弹窗
    move(e) {
      if (this.isMove) {
        let X = e.x
        let Y = e.y
        if (X < this.minX) X = this.minX
        else if (X > this.maxX) X = this.maxX
        if (Y < this.minY) Y = this.minY
        else if (Y > this.maxY) Y = this.maxY
        this.$refs.popup.style.left = X - this.minX + this.$refs.popup.offsetWidth / 2 + 'px'
        this.$refs.popup.style.top = Y - this.minY + this.$refs.popup.offsetHeight / 2 + 'px'
      }
    },
    // 松开鼠标取消移动
    up() {
      this.isMove = false
    },
    // 文本内容
    content(text) {
      this.popupShow = true
      this.text = text
    }
  },
  // 使用事件总线接受数据
  mounted() {
    this.$bus.$on('popupContent', this.content)
  },
  beforeDestroy() {
    this.$bus.$off('popupContent') // 用 $off去解绑当前组件所用到的事件，不要在事件总线 $bus中占事件命名位置
  }
}
</script>

<style scoped lang="less">
.popup_container {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 99;
  width: 260px;
  border-radius: 2px;
  background-color: #fff;

  .popup_title {
    padding: 0 80px 0 20px;
    line-height: 42px;
    border-bottom: 1px solid #eee;
    font-size: 16px;
    color: #333;
    overflow: hidden;
    background-color: #f8f8f8;
    cursor: move;
  }

  .popup_content {
    padding: 20px;
    line-height: 24px;
    font-size: 16px;
  }

  .popup_determine {
    float: right;
    margin-right: 20px;
    margin-bottom: 12px;
    background-color: #0047ae;
    cursor: pointer;
    border-radius: 2px;
    text-decoration: none;

    span {
      padding: 0 15px;
      color: #fff;
      line-height: 28px;
      font-weight: 400;
      font-size: 16px;
    }
  }

  .popup_x {
    position: absolute;
    width: 16px;
    height: 16px;
    right: 20px;
    top: 15px;
    z-index: 100;
    cursor: pointer;

    &::before {
      position: absolute;
      content: '|';
      transform: rotate(45deg);
      left: 9px;
      top: 0;
    }

    &::after {
      position: absolute;
      content: '|';
      transform: rotate(-45deg);
      left: 8px;
      top: 0;
    }
  }
}

.popup_cover {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 98;
}
</style>